/**
 * 
 */
package neptune.web.client.commands;

import neptune.web.client.model.SerializableExperiment;
import neptune.web.client.services.experiment.ExperimentService;
import neptune.web.client.view.ConsoleManager;
import neptune.web.client.view.ExperimentView;
import neptune.web.client.view.HistoryTokenUtils;
import neptune.web.client.view.OperativeView;
import neptune.web.client.view.SuperView;
import neptune.web.client.view.ViewManager;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class OpenExperimentViewCommand implements Command {

	private String experimentId;

	public OpenExperimentViewCommand(String expId) {
		experimentId = expId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.Command#execute()
	 */
	public void execute() {
		SuperView operativeView = (SuperView) ViewManager.getViewManager()
				.getViewByName(OperativeView.DEFAULT_TOKEN);
		final ExperimentView expView = (ExperimentView) operativeView
				.getViewByName(ExperimentView.DEFAULT_TOKEN);

		ExperimentService.Util.getInstance().getExperiment(experimentId,
				new AsyncCallback<SerializableExperiment>() {

					public void onSuccess(SerializableExperiment exp) {
						expView.setExperimentData(exp);

						ConsoleManager.addConsoleMessage("Experiment '"
								+ experimentId + "' infos retrieved.", false);
					}

					public void onFailure(Throwable arg0) {
						ConsoleManager.addConsoleMessage(
								"Impossible to retrieve experiment '"
										+ experimentId + "' infos. Cause: "
										+ arg0.getMessage(), true);
					}

				});
		ExperimentService.Util.getInstance().getXMLTopologyOfExperiment(experimentId,
				new AsyncCallback<String>()
				{
		
					public void onFailure(Throwable caught)
					{
						ConsoleManager.addConsoleMessage(
								"Impossible to retrieve XML experiment topology '"
										+ experimentId + "' infos. Cause: "
										+ caught.getMessage(), true);
						
					}
		
					public void onSuccess(String result) 
					{
						expView.setXMLexperimentTopology(result);
						ConsoleManager.addConsoleMessage("XML experiment topology '"
								+ experimentId + "' infos retrieved.", false);
					}
				});
		

		expView.setExpetimentTabActive();
		History.newItem(HistoryTokenUtils.buildToken(
				OperativeView.DEFAULT_TOKEN, ExperimentView.DEFAULT_TOKEN));

	}

}
